package com.arm.controller.login;


import com.arm.controller.BaseController;
import com.arm.def.Constant;
import com.arm.tools.ValidateCode;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Baggio on 2018-6-22.
 */
@RestController
@RequestMapping("/login")
public class LoginController extends BaseController {

    /**
     * 获取验证码
     */
    @GetMapping(value = "/getVerificatioCode")
    public void getVerificationCode(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片
        response.setHeader("Pragma", "No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        ValidateCode vCode = new ValidateCode(160, 40, 5, 150);
        try {
            String key = Constant.SESSION_KEY_RANDOM_VALIDATE_CODE;
            super.session.setAttribute(key, vCode.getCode());
            logger.info("随机验证码：" + vCode.getCode());
            vCode.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping(value = "/login")
    public ResultObj login(String userName, String passWord, String code) {
        ResultObj resultObj = new ResultObj();
        logger.info(userName + "===================>>>" + passWord);
        try {

        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj;
    }

    @PostMapping(value = "/logOut")
    public ResultObj logOut() {
        ResultObj resultObj = new ResultObj();
        try {
            super.session.invalidate();
        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj;
    }

    @RequestMapping(value = "/gotoLogin")
    public ModelAndView gotoLogin() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("login/login");
        return mov;
    }

    @RequestMapping(value = "/loginTimeOut", method = RequestMethod.GET)
    public ModelAndView loginTimeOut() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("login/loginTimeOut");
        return mov;
    }


}
